package com.kzj.mls.recyclerCard.cardView;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kzj.mls.R;
import com.kzj.mls.global.App;
import com.kzj.mls.recyclerCard.basic.CardItemView;
import com.kzj.mls.recyclerCard.card.other.EvaluationOrderCard;
import com.kzj.mls.util.ImageUtil;

import java.util.ArrayList;

/**
 * Created by xyg on 2015/10/26.
 */
public class EvaluationOrderCardView extends CardItemView<EvaluationOrderCard> implements View.OnClickListener {

    private Context context;
    private TextView textView_type;
    private ImageView imageView_logo;
    private TextView textView_goods_name;
    private TextView textView_goods_amount;
    private TextView textView_order_amount;
    private ImageView imageView_start_project1;
    private ImageView imageView_start_project2;
    private ImageView imageView_start_project3;
    private ImageView imageView_start_project4;
    private ImageView imageView_start_project5;
    private ImageView imageView_start_store1;
    private ImageView imageView_start_store2;
    private ImageView imageView_start_store3;
    private ImageView imageView_start_store4;
    private ImageView imageView_start_store5;
    private EditText editText_content;
    private boolean[] project = new boolean[5];
    private boolean[] store = new boolean[5];
    private ArrayList<ImageView> projectStarts = new ArrayList<>();
    private ArrayList<ImageView> storeStarts = new ArrayList<>();

    public EvaluationOrderCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public EvaluationOrderCardView(Context context) {
        super(context);
        this.context = context;
    }

    public EvaluationOrderCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void build(final EvaluationOrderCard card) {
        super.build(card);
        card.setProject(project);
        card.setStore(store);
        textView_type = (TextView) findViewById(R.id.textView_type);
        imageView_logo = (ImageView) findViewById(R.id.imageView_logo);
        textView_goods_name = (TextView) findViewById(R.id.textView_goods_name);
        textView_goods_amount = (TextView) findViewById(R.id.textView_goods_amount);
        textView_order_amount = (TextView) findViewById(R.id.textView_order_amount);
        imageView_start_project1 = (ImageView) findViewById(R.id.imageView_start_project1);
        imageView_start_project2 = (ImageView) findViewById(R.id.imageView_start_project2);
        imageView_start_project3 = (ImageView) findViewById(R.id.imageView_start_project3);
        imageView_start_project4 = (ImageView) findViewById(R.id.imageView_start_project4);
        imageView_start_project5 = (ImageView) findViewById(R.id.imageView_start_project5);
        imageView_start_store1 = (ImageView) findViewById(R.id.imageView_start_store1);
        imageView_start_store2 = (ImageView) findViewById(R.id.imageView_start_store2);
        imageView_start_store3 = (ImageView) findViewById(R.id.imageView_start_store3);
        imageView_start_store4 = (ImageView) findViewById(R.id.imageView_start_store4);
        imageView_start_store5 = (ImageView) findViewById(R.id.imageView_start_store5);
        editText_content = (EditText) findViewById(R.id.editText_content);
        projectStarts.add(imageView_start_project1);
        projectStarts.add(imageView_start_project2);
        projectStarts.add(imageView_start_project3);
        projectStarts.add(imageView_start_project4);
        projectStarts.add(imageView_start_project5);
        storeStarts.add(imageView_start_store1);
        storeStarts.add(imageView_start_store2);
        storeStarts.add(imageView_start_store3);
        storeStarts.add(imageView_start_store4);
        storeStarts.add(imageView_start_store5);
        imageView_start_project1.setOnClickListener(this);
        imageView_start_project2.setOnClickListener(this);
        imageView_start_project3.setOnClickListener(this);
        imageView_start_project4.setOnClickListener(this);
        imageView_start_project5.setOnClickListener(this);
        imageView_start_store1.setOnClickListener(this);
        imageView_start_store2.setOnClickListener(this);
        imageView_start_store3.setOnClickListener(this);
        imageView_start_store4.setOnClickListener(this);
        imageView_start_store5.setOnClickListener(this);
        changeStart();
        textView_goods_amount.setText(card.getOrderBean().getGoods_amount());
        //textView_order_amount.setText("实际支付：" + card.getOrderBean().getOrder_amount() + "元 " + "(" + card.getOrderBean().getPayment_name() + ")");
        textView_goods_name.setText(card.getOrderBean().getGoods_name());
        if (card.getOrderBean().getSite_type().equals("1")) {
            textView_type.setText(card.getOrderBean().getSite_type_lang());
        } else {
            //textView_type.setText(card.getOrderBean().getSite_type_lang() + "：" + card.getOrderBean().getSeller_name());
        }
        App.imageLoader.displayImage(card.getOrderBean().getGoods_image(), imageView_logo, ImageUtil.OptionsNormal());
    }

    private void changeStatus(boolean[] booleans, int flag) {
        for (int i = 0; i < booleans.length; i++) {
            if (i < flag) {
                booleans[i] = true;
            } else {
                booleans[i] = false;
            }
        }
    }

    private void changeStart() {
        for (int i = 0; i < project.length; i++) {
            if (project[i]) {
                App.imageLoader.displayImage("drawable://" + R.drawable.start_1, projectStarts.get(i), ImageUtil.OptionsNormal());
            } else {
                App.imageLoader.displayImage("drawable://" + R.drawable.start_2, projectStarts.get(i), ImageUtil.OptionsNormal());
            }
        }

        for (int i = 0; i < store.length; i++) {
            if (store[i]) {
                App.imageLoader.displayImage("drawable://" + R.drawable.start_1, storeStarts.get(i), ImageUtil.OptionsNormal());
            } else {
                App.imageLoader.displayImage("drawable://" + R.drawable.start_2, storeStarts.get(i), ImageUtil.OptionsNormal());
            }
        }
        editText_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                getCard().setContent(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_start_project1:
                changeStatus(project, 1);
                changeStart();
                break;
            case R.id.imageView_start_project2:
                changeStatus(project, 2);
                changeStart();
                break;
            case R.id.imageView_start_project3:
                changeStatus(project, 3);
                changeStart();
                break;
            case R.id.imageView_start_project4:
                changeStatus(project, 4);
                changeStart();
                break;
            case R.id.imageView_start_project5:
                changeStatus(project, 5);
                changeStart();
                break;
            case R.id.imageView_start_store1:
                changeStatus(store, 1);
                changeStart();
                break;
            case R.id.imageView_start_store2:
                changeStatus(store, 2);
                changeStart();
                break;
            case R.id.imageView_start_store3:
                changeStatus(store, 3);
                changeStart();
                break;
            case R.id.imageView_start_store4:
                changeStatus(store, 4);
                changeStart();
                break;
            case R.id.imageView_start_store5:
                changeStatus(store, 5);
                changeStart();
                break;
        }
    }
}
